<?php

namespace app\admin\controller\property;

use app\common\controller\Backend;
use think\Db;

/**
 * 物品入库
 *
 * @icon fa fa-circle-o
 */
class Repertorys extends Backend
{

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Vindicate;
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                //->where('admin_id', 'in', [0])
                ->order('id', 'desc')
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }


    /**
     * 详情
     */
    public function detail($ids)
    {


        //$row = $this->model->get(['id' => $ids]);
        $row = Db::table('fa_vindicate')
            ->where(['id'=>['in',$ids]])
            ->order('id','desc')
            ->select();

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 入库
     */
    public function edits(){
        if ($this->request->isPost()) {
            $row = $this->request->post("row/a", [], 'trim');
            if ($row) {
                $arr = [];
                foreach ($row['counts'] as $k=>$v){
                    $arr[$k]['vindicate_id'] = $k;
                    $arr[$k]['count'] = $v;
                    $arr[$k]['counts'] = $v;
                    $arr[$k]['price'] = $row['price'][$k];
                    $arr[$k]['create_time'] = time();
                }

                $arr = array_merge($arr);

                $insert = Db::table('fa_repertory')
                    ->insertAll($arr);

                if ($insert > 0){
                    $this->success('成功');
                }else{
                    $this->error(__('失败'));
                }


            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
    }


}
